package com.fl.springcloud.controller;

import com.fl.springcloud.entities.CommonResult;
import com.fl.springcloud.entities.PayMent;
import com.fl.springcloud.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
public class PaymentController {
    @Resource
    private PaymentService service;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping(value = "/getPayment/{id}")
    public CommonResult<PayMent> getPayment(@PathVariable("id") Long id ){
        PayMent paymentById = service.getPaymentById(id);
        if (paymentById!=null){
          return   new CommonResult(200,"获取成功！"+serverPort,paymentById);
        }
        CommonResult<PayMent> result = new CommonResult(500, "获取失败！=>id:"+id,null);
        return result;
    }

    @PostMapping(value = "/payment/save")
    public CommonResult save(@RequestBody PayMent payMent){
        int save = 0;
        System.out.println(StringUtils.isEmpty(payMent.getSerial()));
        if (!StringUtils.isEmpty(payMent.getSerial())){
             save = service.save(payMent);
        }
        if (save>0){
            return new  CommonResult(200,"添加订单成功!",payMent);
        }
        return new CommonResult(500, "添加订单失败！");
    }

    @GetMapping(value = "/payment/feign/timeout")
    public String PaymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

    @GetMapping(value = "payment/hystrix/ok/{id}")
    public String payment_ok(@RequestBody @PathVariable("id") Integer id){
        String s = service.payment_ok(id);
        return s;
    }


    @GetMapping(value = "payment/hystrix/timeout/{id}")
    public String payment_timeout(@RequestBody @PathVariable("id") Integer id){
        String timeout = service.payment_timeout(id);
        return timeout;
    }
    @GetMapping(value = "payment/hystrix/paymentCircuitBreaker/{id}")
    public String paymentCircuitBreaker(@PathVariable("id") Integer id){
        String breaker = service.paymentCircuitBreaker(id);
        return breaker;
    }
    //服务链路调用，可观察服务被谁调用最终执行服务是谁！
    @GetMapping(value = "payment/zipkin")
    public void zipkin(){
        System.out.println("hello i am zipkin");
    }
}
